package com.example.codegen;

public class SpawnStatement implements Statement {
    /// use to store thread handler, wait for all handle to complete in fuction.
    public static String threadsPool = "ScopeLocalThreadsPool";
    public static String goWaitGroup = "wg";
    public SpawnExpression spawnExpression;

    public SpawnStatement(SpawnExpression spawnExpression) {
        this.spawnExpression = spawnExpression;
    }

    @Override
    public String toJava() {
        return threadsPool + ".add(" + spawnExpression.toJava() + ");\n" + threadsPool + ".get(" + threadsPool + ".size()-1).start();";
    }

    @Override
    public String toGo() {
        return goWaitGroup + ".Add(1)\n" + "go func() {\n" + "defer " + goWaitGroup + ".Done()\n" + spawnExpression.toGo() + "\n" + "}()";
    }

    @Override
    public String toJs() {
        // FIXME(wy): use worker_threads to spawn really thread
        return "new Promise((resolve, reject) => { " + spawnExpression.toJs() + " })";
    }

    @Override
    public String toCSharp() {
        return threadsPool + ".Add(" + spawnExpression.toCSharp() + ");\n" + threadsPool + ".Last().Start();";
    }

    @Override
    public String toCJ() {
        return "futureList.append(" + spawnExpression.toCJ() + ")";
    }
}
